'''
 * @ author     ：廖传港
 * @ date       ：Created in 2020/11/6 11:14
 * @ description：
 * @ modified By：
 * @ ersion     : 
 * @File        : train.py 
'''
import numpy as np
from com.lcg.version5 import trainModelByTeacher2 as tr
from com.lcg.version5 import Loading_pictures as lp

if __name__ == '__main__':
    # 加载数据集
    X, Y = lp.loaddata("D:/python/data/")

    dnn=tr.DNN()
    #卷积
    dnn.Add(tr.CNN2D_MultiLayer(4,4,stride=2,nFilter=10))
    #池化
    dnn.Add(tr.DMaxPooling2D(2,2))

    #卷积
    dnn.Add(tr.CNN2D_MultiLayer(4,4,stride=2,nFilter=2))
    #池化
    dnn.Add(tr.DMaxPooling2D(2,2))

    #拉平张量，就是把二维变一维
    dnn.Add(tr.DFlatten())

    #全连接，输出50，使用sigmoid激活函数
    # dnn.Add(tr.DDense(50,'sigmoid'))
    # 全连接
    dnn.Add(tr.DDense(100,50,'relu'))
    # 全连接
    dnn.Add(tr.DDense(10,'relu'))


    # dnn.AdjustWeightRatio(5)
    # dnn.Add(DDense(100,10,'linear'))

    # ratio=dnn.AdjustWeightsRatio(X,YY)


    # dnn.Add(DDense(10,1,'linear'))

    #匹配损失函数
    dnn.Compile(lossMethod='SoftmaxCrossEntropy')

    # ratio=dnn.AdjustWeightsRatio(X,YY)


    # 批预测
    yy=dnn.BatchPredict(X[200:250,:])
    print(yy)
    # print("X[0:80,:]:",X[0:80,:])
    # dnn.Fit(X[0:80,:], Y[0:80],100)
    dnn.Fit(X[0:200,:], Y[0:200,],100)

    dnn = tr.DNN()

    dnn.Add(tr.CNN2D(6, 6, stride=2, nFilter=10))

    dnn.Add(tr.DMaxPooling2D(2, 2))

    # yy=dnn.Forward(X[0])

    dnn.Add(tr.DFlatten())

    # yy=dnn.Forward(X[0])

    dnn.Add(tr.DDense(80, 'sigmoid', bFixRange=True))

    # dnn.Add(DDense(100,50,'relu'))

    dnn.Add(tr.DDense(10, 'relu', bFixRange=True))

    # dnn.AdjustWeightRatio(5)
    # dnn.Add(DDense(100,10,'linear'))

    # ratio=dnn.AdjustWeightsRatio(X,YY)


    # dnn.Add(DDense(10,1,'linear'))


    dnn.Compile(lossMethod='SoftmaxCrossEntropy')

    # ratio=dnn.AdjustWeightsRatio(X,YY)

    # yy=dnn.BatchPredict(X)

    dnn.Fit(X[0:150, :], Y[0:150, :], 200)

    # predictY：预测Y BatchPredict批预测

    predictY = dnn.BatchPredict(X[150:200, ])

    predictYY = np.array([np.argmax(one_hot) for one_hot in predictY])

    realY = Y[150:200, ]

    realYY = np.array([np.argmax(one_hot) for one_hot in realY])

    from sklearn.metrics import accuracy_score

    accuracy_score(predictYY, realYY)
    # realy=Y[180:200,]
    #
    # nx=yy[0]
    #
    # ny=realy[0]
    # loss = np.sum(- ny * np.log(nx))

    # crossE=CrossEntropy()
    #
    # loss=crossE.loss(yy[0],realy[0])